package com.liming.service;

import java.sql.SQLException;
import java.util.List;

import com.liming.dao.Goodsdao;
import com.liming.dao.Ordersdao;
import com.liming.model.Goods;
import com.liming.model.Orders;


public class OrdersService {

Ordersdao osdao = new Ordersdao();
	
	public boolean createOrder(Orders orders) {
		
		if(osdao.createOrder(orders)!=0) {
			System.out.println("创建订单成功");
			return true;
		}
		else {
			System.out.println("创建订单失败");
			return false;
		}
		
	}

	public boolean cancelOrder(int userid, String ordernum) {
		
		if(osdao.cancelOrder(userid,ordernum)!=0) {
			System.out.println("删除订单成功");
			return true;
		}
		else {
			System.out.println("删除订单失败");
			return false;
		}
		
	}

	
	public boolean payOffOrder(int userid, String ordernum,int state) {
		
		if(osdao.payOffOrder(userid,ordernum,state)!=0) {
			System.out.println("结算订单成功");
			return true;
		}
		else {
			System.out.println("结算订单失败");
			return false;
		}
		
	}

	
	public List<Orders> getAllOrder(int userid) {
		
		return osdao.getAllOrder(userid);
		
	}

	public List<Orders> filterOrder(int userid, String keyword) {
		
		Goodsdao goodsdao = new Goodsdao();
		
		List<Orders> allorders = osdao.getAllOrder(userid);
		List<Orders> orders = null;
		for(Orders order : allorders)
		{
			//根据Order-->id调用goodsinfo类方法查goodsn
			Goods goods = Goods.NULLGOODS;
			try {
				goods = goodsdao.getGoodsByid(order.getGoodsid());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (goods.getGoodsname().contains(keyword)) {
				orders.add(order);
			}
		}
		
		return orders;
	}
	
}
